package com.eastcom.groovy.process;

import javax.annotation.Resource

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
import org.springframework.stereotype.Component

import com.eastcom.groovy.config.TaskConfigPath
import com.eastcom.groovy.context.ServerContext
import com.eastcom.groovy.distributor.DPService;
import com.eastcom.groovy.provider.DynamicBeanDomianProvider
import com.eastcom.groovy.provider.DynamicBeanRegisterProvider
import com.eastcom.groovy.provider.PropertyPlaceholderConfigurerProvider
import com.eastcom.groovy.provider.SpringContextProvider

@Component("serverInitProcess")
public class ServerInitProcess {
	private static Log logger = LogFactory.getLog(ServerInitProcess.class);
	@Resource(name = "threadPool")
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;
	public void init(){
		try {
			DynamicBeanRegisterProvider beanRegister = SpringContextProvider.getBean("beanRegister")
			beanRegister.setApplicationContext(SpringContextProvider.getApplicationContext())
			DynamicBeanDomianProvider.init()
			String serverInitShell = PropertyPlaceholderConfigurerProvider.getContextProperty("server_shell")
			if(serverInitShell != null){
				File f = new File(serverInitShell)
				if(f.isFile()){
					Binding binding = new Binding()
					binding.setVariable("flag","startDerbyDataBase")
					GroovyShell shell = new GroovyShell(binding)
					Script script = shell.parse(new File(serverInitShell))
					script.run()
				}
			}
			DPService dPService = SpringContextProvider.getBean("dpService");
			dPService.startService()
			TaskConfigPath config = ServerContext.serverPath.getTaskConfigPath()
			TaskInitProcess taskInitProcess = SpringContextProvider.getBean("taskInitProcess");
			List path = new ArrayList();
			path.addAll(config.taskPath);
			taskInitProcess.init(path);
		} catch (Exception e) {
			e.printStackTrace()
		}
	}
}
